package com.example.crowdfunding.mvc.Controller;

import com.example.crowdfunding.constant.CrowdConstant;
import com.example.crowdfunding.entity.Role;
import com.example.crowdfunding.service.api.RoleService;
import com.example.crowdfunding.utils.ResultEntity;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * The type Role controller.
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * The Logger.
     */
    Logger logger = LoggerFactory.getLogger(AdminController.class);

    @RequestMapping(value = "/do/delRole.json")
    @ResponseBody
    ResultEntity<String> removeRoleSByIds(@RequestBody List<Integer> roleIdList) {

        if (roleIdList == null || roleIdList.size() == 0) {
            throw new RuntimeException(CrowdConstant.MESSAGE_INPUT_EDIT_MISSING_PARAMETERS);
        }
        roleService.removeRolesByIds(roleIdList);
        return ResultEntity.successWithoutData(CrowdConstant.MESSAGE_DELETE_ROLE_SUCCESS);
    }

    /**
     * Gets page info.
     *
     * @param pageNum  the page num
     * @param pageSize the page size
     * @param keyword  the keyword
     * @return the page info
     */
    @RequestMapping(value = "/get/roleList.json")
    @ResponseBody
    public ResultEntity<PageInfo<Role>> getPageInfo(
            // 因为都有默认值，所以不需要判空
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "8") Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "") String keyword
    ) {

        try {
            PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);
            logger.debug("role分页信息：{}", pageInfo);
            return ResultEntity.successWithData(pageInfo, CrowdConstant.MESSAGE_RESPONSE_SERVER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(CrowdConstant.MESSAGE_SYSTEM_ERROR + " " + e.getMessage());
        }
    }

    /**
     * Add role
     *
     * @param role the role
     * @return the result entity
     */
    @RequestMapping(value = "/do/addRole.json")
    @ResponseBody
    public ResultEntity<String> addRole(Role role) {

        if (role != null) {
            roleService.addRole(role);
            return ResultEntity.successWithoutData(CrowdConstant.MESSAGE_SAVE_ROLE_SUCCESS);
        }

        throw new RuntimeException(CrowdConstant.MESSAGE_INPUT_EDIT_MISSING_PARAMETERS);
    }

    /**
     * Update role result entity.
     *
     * @param role the role
     * @return the result entity
     */
    @RequestMapping(value = "/do/updateRole.json")
    @ResponseBody
    public ResultEntity<String> updateRole(Role role) {

        if (StringUtils.isNullOrEmpty(role.getRoleName())
                || role.getId() == null
                || role.getId() < 0
        ) {
            throw new RuntimeException(CrowdConstant.MESSAGE_INPUT_EDIT_MISSING_PARAMETERS);
        }

        roleService.updateRole(role);

        return ResultEntity.successWithoutData(CrowdConstant.MESSAGE_UPDATE_ROLE_SUCCESS);
    }

}
